﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Mail;
using CE.Global;

public partial class user_editprofile : MyLanguageBasePage
{
    string _redirectUrl = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.Page.Title = Resources.Title.EditProfileTitle;
        
        LiteralControl newln = new LiteralControl(Environment.NewLine);
        HtmlMeta language = new HtmlMeta();
        language.Name = "language";
        language.Content = Resources.Title.PageLanguage;

        HtmlMeta keywords = new HtmlMeta();
        keywords.Name = "keywords";
        keywords.Content = Resources.Title.EditProfileKeyword;

        HtmlMeta description = new HtmlMeta();
        description.Name = "description";
        description.Content = Resources.Title.EditProfileDescription;

        Page.Header.Controls.AddAt(10, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(11, language);
        Page.Header.Controls.AddAt(12, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(13, keywords);
        Page.Header.Controls.AddAt(14, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(15, description);

       
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.Customer) && Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.Provider))
            {
                _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/car-and-policy-details.aspx", Page);
                Response.Redirect(_redirectUrl);
            }
        }
        else
        {
            _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/car-and-policy-details.aspx", Page);
            Response.Redirect(_redirectUrl);
        }


        if (!Page.IsPostBack)
        {
            btnSubmit.OnClientClick = "javascript:return confirm('" + Resources.General.Confirm + "');";

            usrDOB.StartYear = DateTime.Now.Year - 75;
            usrDOB.EndYear = DateTime.Now.Year - 18;
            //txtSurName.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtSurName.ClientID + "','" + rfvSurName.ClientID + "','" + revSurname.ClientID + "');");
            ShowUserDetails();
        }
        LogUserViewedHistory();
        Page.Form.DefaultButton = btnSubmit.UniqueID;
    }

    private void LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        _userviewdb.LogViewedHistory("USR", Request, "User edit profile.");
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        //Salutation
        //ddlSalutation.ChangeValidationEffectServerSide(strValidationGroup);
        if (revFirstName.Enabled && revFirstName.ValidationGroup == strValidationGroup)
        {
            revFirstName.Validate();
            if (revFirstName.IsValid == false)
            {
                txtFirstName.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtFirstName.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtFirstName.ControlStyle.CssClass = "";
        }

        //Surname
        //if (rfvSurName.Enabled && rfvSurName.ValidationGroup == strValidationGroup)
        //{
        //    rfvSurName.Validate();
        //    if (rfvSurName.IsValid == false)
        //    {
        //        txtSurName.ControlStyle.CssClass = "validationError";
        //    }
        //    else
        //    {
        //        txtSurName.ControlStyle.CssClass = "";
        //    }
        //}
        //else
        //{
        //    txtSurName.ControlStyle.CssClass = "";
        //}

        //Date of birth
        usrDOB.ChangeValidationEffectServerSide(strValidationGroup);

        //Gender
        ddlGender.ChangeValidationEffectServerSide(strValidationGroup);

        //Salutation and Gender
        //if (csvSalGender.Enabled && csvSalGender.ValidationGroup == strValidationGroup)
        //{
        //    if (ddlSalutation.IsValidRequirement() && ddlGender.IsValidRequirement())
        //    {
        //        csvSalGender.Validate();
        //        if (csvSalGender.IsValid == false)
        //        {
        //            ddlGender.ChangeValidateEffect(true);
        //            ddlSalutation.ChangeValidateEffect(true);
        //        }
        //        else
        //        {
        //            ddlGender.ChangeValidateEffect(false);
        //            ddlSalutation.ChangeValidateEffect(false);
        //        }
        //    }
        //}
        //else
        //{
        //    ddlGender.ChangeValidateEffect(false);
        //    ddlSalutation.ChangeValidateEffect(false);
        //}

        //Nationality
        //ddlNationality.ChangeValidationEffectServerSide(strValidationGroup);

        //Mobile Number
        txtMobileNo.ChangeValidationEffectServerSide(strValidationGroup);

        //Type Of Indentification
        //ddlIDType.ChangeValidationEffectServerSide(strValidationGroup);
        //ID Number
        //txtIdNumber.ChangeValidationEffectServerSide(strValidationGroup);

        //ID Prefix & ID Type
        //if (csvIdTypePrefix.Enabled && csvIdTypePrefix.ValidationGroup == strValidationGroup)
        //{
        //    if (ddlIDType.IsValidRequirement() && txtIdNumber.IsValidePrefixRequirement())
        //    {
        //        csvIdTypePrefix.Validate();
        //        if (csvIdTypePrefix.IsValid == false)
        //        {
        //            ddlIDType.ChangeValidateEffect(true);
        //            txtIdNumber.ChangeValidateEffectPrefix(true);
        //            csvIdTypePrefix.ErrorMessage = "Identification type and NRIC/FIN number's prefix character are not matched.";
        //        }
        //        else
        //        {
        //            ddlIDType.ChangeValidateEffect(false);
        //            txtIdNumber.ChangeValidateEffectPrefix(false);
        //        }
        //    }
        //}
        //else
        //{
        //    ddlIDType.ChangeValidateEffect(false);
        //    txtIdNumber.ChangeValidateEffectPrefix(false);
        //}

        //if (rfvIdNumber.Enabled && rfvIdNumber.ValidationGroup == strValidationGroup)
        //{
        //    rfvIdNumber.Validate();
        //    if (rfvIdNumber.IsValid == false)
        //    {
        //        txtIdNumber.ControlStyle.CssClass = "validationError";
        //    }
        //    else
        //    {
        //        txtIdNumber.ControlStyle.CssClass = "";
        //    }
        //}
        //else
        //{
        //    txtIdNumber.ControlStyle.CssClass = "";
        //}

        ////Year of Driving In SG
        //if (rfvYearDriveSg.Enabled && rfvYearDriveSg.ValidationGroup == strValidationGroup)
        //{
        //    rfvYearDriveSg.Validate();
        //    if (rfvYearDriveSg.IsValid == false)
        //    {
        //        ddlYearDriveSg.ControlStyle.CssClass = "validationError";
        //    }
        //    else
        //    {
        //        ddlYearDriveSg.ControlStyle.CssClass = "";
        //    }
        //}
        //else
        //{
        //    ddlYearDriveSg.ControlStyle.CssClass = "";
        //}

        //if (csvAgeAndYear.Enabled && csvAgeAndYear.ValidationGroup == strValidationGroup)
        //{
        //    csvAgeAndYear.Validate();
        //    if (csvAgeAndYear.IsValid == false)
        //    {
        //        ddlYearDriveSg.ControlStyle.CssClass = "validationError";
        //        usrDOB.ChangeValidationEffect();
        //    }
        //    else
        //    {
        //        ddlYearDriveSg.ControlStyle.CssClass = "";
        //    }
        //}
        //else
        //{
        //    ddlYearDriveSg.ControlStyle.CssClass = "";
        //}
    }

    private void ShowUserDetails()
    {
        CE.User.Profile _profile = new CE.User.Profile();
        CE.User.ProfileDB _profiledb = new ProfileDB();
        CE.User.Contact _contact = new Contact();
        CE.User.ContactDB _contactdb = new ContactDB();
        Int64 UserId = Convert.ToInt64(Session[General.UserId].ToString());
        _profile = _profiledb.GetProfile(UserId);
        if (_profile != null)
        {
            //ddlSalutation.Saluation = _profile.Salutation;
            txtFirstName.Text = _profile.FirstName + (_profile.SurName.Trim() != "" ? " " + _profile.SurName.Trim() : "");
            txtSurName.Text = ""; //_profile.SurName;
            //if (_profile.FirstName != string.Empty)
            //{
            //    txtFirstName.Text = _profile.FirstName;
            //}
            usrDOB.Day = _profile.DOB.Day;
            usrDOB.Month = _profile.DOB.Month;
            usrDOB.Year = _profile.DOB.Year;

            ddlGender.Gender = _profile.Gender.ToString();

            //if (_profile.MaritalStatus != -1)
            //    ddlMaritalStatus.MaritalStatus = _profile.MaritalStatus.ToString();

            if (_profile.Occupation != -1)
                ddlOccupation.Occupation = _profile.Occupation.ToString();

            //if (_profile.JobNature != string.Empty)
            //    ddlJobNature.JobNature = _profile.JobNature;

            //if (_profile.IDType != string.Empty)
            //    ddlIDType.IdType = _profile.IDType;

            //if (_profile.IDNo.Length == 9)
            //{
            //    txtIdNumber.Prefix = _profile.IDNo[0].ToString();
            //    txtIdNumber.Postfix = _profile.IDNo[8].ToString();
            //    txtIdNumber.IDNumber = _profile.IDNo.Substring(1, 7);
            //}
            //txtIdNumber.Text = _profile.IDNo;

            //ddlNationality.Nationality = _profile.Nationality;
            //ddlYearDriveSg.Text = _profile.YearDriveSg.ToString();

            _contact = _contactdb.GetContact(UserId);
            if (_contact != null)
            {
                txtEmailAddress.Text = _contact.Email;
                txtMobileNo.PhoneNo = _contact.Mobile;
                //txtHomePhoneNo.PhoneNo = _contact.HomePhone;
                //txtOfficeNo.PhoneNo = _contact.OfficePhone;
            }

            //if (Session[CE.Global.General.UserProfileAndContactComplete].ToString() == "1")
            //{
            //    usrDOB.IsEnabled = false;
            //    ddlGender.IsEnabled = false;
            //}
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        ChangeValidationEffectServerSide("profile");
        if (Page.IsValid)
        {
            txtFirstName.Text = CE.Global.General.ClearScriptTag(txtFirstName.Text);
            txtSurName.Text = CE.Global.General.ClearScriptTag(txtSurName.Text);
            //txtFirstName.Text = CE.Global.General.ClearScriptTag(txtFirstName.Text);
            Int64 _userId = Convert.ToInt64(Session[General.UserId].ToString());
            Profile _userprofile = null;
            ProfileDB _userprofiledb = new ProfileDB();
            Contact _contact = null;
            ContactDB _contactdb = new ContactDB();

            if (_userId > 0)
            {
                _userprofile = _userprofiledb.GetProfile(_userId);
                if (_userprofile == null)
                {
                    _userprofile = new Profile();
                }

                //_userprofile.Salutation = ddlSalutation.GetSalutation();
                _userprofile.FirstName = Server.HtmlEncode(txtFirstName.Text);
                _userprofile.SurName = Server.HtmlEncode(txtSurName.Text);
                _userprofile.DOB = usrDOB.GetDate();
                _userprofile.Gender = Convert.ToChar(ddlGender.GetGender());

                //_userprofile.MaritalStatus = Convert.ToInt32(ddlMaritalStatus.GetMaritalStatus());
                //_userprofile.JobNature = ddlJobNature.GetJobNature();
                _userprofile.Occupation = Convert.ToInt32(ddlOccupation.GetOccupation());

                _userprofile.IDType = "";// ddlIDType.GetIDType();
                //_userprofile.IDNo = txtIdNumber.Text;//txtIdNumber.GetIdNumber();

                //_userprofile.Nationality = ddlNationality.GetNationality();
                //_userprofile.YearDriveSg = Convert.ToInt16(ddlYearDriveSg.Text);

                _userprofile.RefCEUserId = _userId;
                _userprofile.Status = "Complete";

                Int64 _profileresult = _userprofiledb.Insert(_userprofile);

                _contact = _contactdb.GetContact(_userId);
                if (_contact == null)
                {
                    _contact = new Contact();
                }

                _contact.Mobile = Server.HtmlEncode(txtMobileNo.GetPhoneNo());
                _contact.Email = Server.HtmlEncode(txtEmailAddress.Text);
                //_contact.HomePhone = txtHomePhoneNo.GetPhoneNo();
                //_contact.OfficePhone = txtOfficeNo.GetPhoneNo();
                _contact.Status = true;
                _contact.RefCEUserId = _userId;
                if (Session[General.AgentId] != null)
                {
                    _contact.Info2 = Session[General.AgentId].ToString();
                    _contact.Info3 = DateTime.Now.ToString();
                }

                Int64 _contactresult = _contactdb.Insert(_contact);

                if (_profileresult > 0 && _contactresult > 0)
                {
                    if (txtFirstName.Text != string.Empty)
                        Session[General.UserName] = (txtSurName.Text.Trim() != "" ? txtSurName.Text + " " : "") + txtFirstName.Text.Trim();
                    else
                        Session[General.UserName] = txtSurName.Text;

                    Session[General.UserPhoneNo] = txtMobileNo.GetPhoneNo();
                    if (_userprofile.YearDriveSg > 0)
                    {
                        Session[CE.Global.General.UserProfileAndContactComplete] = "1";
                    }

                    string _urlPath = "~/home.aspx";
                    try
                    {
                        if (Page.Request.Params["redir"] != null)
                        {
                            string _tempUrlPath = GetRedirectUrl(Page.Request.Params["redir"].ToString());
                            string testUrl = Server.MapPath(_tempUrlPath);
                            _urlPath = Server.UrlDecode(_tempUrlPath);
                        }
                    }
                    catch (Exception Ex)
                    {
                        Ex = null;
                        _urlPath = "~/home.aspx";
                    }
                    finally
                    {
                        lblSetInsMessage.Text = Resources.General.ChangeUpdated; //"Your changes is updated.";
                        _redirectUrl = CE.Global.General.GetUrl(_urlPath, Page);
                        //Response.Redirect(_redirectUrl);
                    }
                }
            }
        }
    }

    private string GetRedirectUrl(string strPathId)
    {
        string retValue = "";
        retValue = "~/";
        return retValue;
    }
}
